package com.jie.deletefile.DongtaiVp;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.xutils.x;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/12/5.
 */
public class DVpUtil implements ViewPager.OnPageChangeListener {
    private Context context;
    private ViewPager vp;
    private View[] dotLists;//下标数组
    private List<String> data;//图片地址集合
    private ScheduledExecutorService service;


    public DVpUtil(Context context, View[] dotLists, ViewPager vp, List<String> data) {
        this.context = context;
        this.dotLists = dotLists;
        this.vp = vp;
        this.data = data;
    }

    private int currentItem = 0;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            vp.setCurrentItem(currentItem);

        };
    };

    public void show() {
        vp.setAdapter(new PagerAdapter() {
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }


            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView view = new ImageView(context);
                x.image().bind(view, data.get(position));
                container.addView(view);
                return view;
            }

            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
        startAd();
        vp.addOnPageChangeListener(this);
    }

    //自动轮播
    private void startAd() {
        service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new ScrollTask(), 3, 5, TimeUnit.SECONDS);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //// TODO: 2016/12/5 下标选中时的状态
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class ScrollTask implements Runnable {

        @Override
        public void run() {
            try {
                synchronized (vp) {
                    currentItem = (currentItem + 1) % data.size();
                    // currentItem = (currentItem + 1) % imageViews.size();
                    handler.obtainMessage().sendToTarget();
                }
            } catch (Throwable e) {
                // TODO: handle exception
            }

        }
    }


}
